get object attributes python

37

python get object attribute by string -

x = getattr(t, 'attr1')
setattr(t, 'attr1', 21)

python get attributes of class -

import inspect

class myclass:
  a = 5
  def method(b):
    return b

for i in inspect.getmembers(myclass):
  print(i)

python get attributes of object -

field_name = "fullName"
print getattr(user, field_name) # prints content of user.fullName

Comments

Submit
0 Comments